<template>
  <div ref="salesProductPie" class="sales-product-pie"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import resize from '@u/resize'

export default {
  name: 'SalesProductPie',
  mixins: [resize],
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      let option = {
        tooltip: {
          trigger: 'item'
        },

        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '80%',
            center: ['50%', '50%'],
            color: ['#5ab1ef', '#b6a2de', '#67e0e3', '#2ec7c9'],
            data: [
              { value: 500, name: '电子产品' },
              { value: 310, name: '服装' },
              { value: 274, name: '化妆品' },
              { value: 400, name: '家居' }
            ].sort(function(a, b) {
              return a.value - b.value
            }),
            roseType: 'radius',
            animationType: 'scale',
            animationEasing: 'exponentialInOut',
            animationDelay: function() {
              return Math.random() * 400
            }
          }
        ]
      }

      this.chart.setOption(option)
    }
  }
}
</script>

<style scoped>
.sales-product-pie {
  height: 300px;
  width: 100%;
}
</style>
